<template>
  <!--求职意向-->
  <view>
    <myHead
      :title="title"
      :right="true"
      text_right="保存"
      @clickRight="clickRight"
    ></myHead>
    <view class="conten">
      <uni-data-picker
        :localdata="items5"
        v-model="zwVal"
        class="unidatapickert"
        popup-title="请选择期望职位"
        ref="picker"
        @change="onchange"
        style="z-index: 100"
      >
      </uni-data-picker>

      <view class="li-box fl-lr-c" @click="showuseddepart()">
        <view class="name">期望职位</view>
        <view class="fl-t">
          <view class="content">
            {{ zwValzhi || "请选择" }}
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>

      <view class="li-box fl-lr-c" @click="ExpectedIndustry">
        <view class="name">期望行业</view>
        <view class="fl-t">
          <view class="content fl-t">
            <template v-for="(item, index) in hyVal">
              <text v-if="hyVal.length" :key="index">
                {{ item.text }}
                <text v-if="hyVal.length - 1 > index"> 、</text>
              </text>
            </template>
            <text v-if="!hyVal.length"
              >{{ IndustryTitles ? IndustryTitles : "不限" }}
            </text>
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>

      <pick-regions2
        @getRegion="handleGetRegion"
        :defaultRegion="defaultRegion"
      >
        <view class="li-box fl-lr-c">
          <view class="name">期望城市</view>
          <view class="fl-t">
            <view class="content">
              {{ city || "请选择" }}
            </view>
            <u-icon name="arrow-right"></u-icon>
          </view>
        </view>
      </pick-regions2>

      <u-picker
        :show="salaryShow"
        title="薪资要求"
        closeOnClickOverlay
        @cancel="salaryShow = false"
        @close="salaryShow = false"
        keyName="text"
        :columns="items_2"
        @confirm="selctChange2"
        ref="uPicker"
        @change="changeHandler"
      >
      </u-picker>

      <view class="li-box fl-lr-c" @click="fnSelect(2)">
        <view class="name">薪资要求</view>
        <view class="fl-t">
          <view class="content" v-if="salaryMin && salaryMax">
            {{ salaryMin == "面议" ? "面议" : `${salaryMin}~${salaryMax}` }}
          </view>
          <view v-else class="content"> 请选择 </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>

      <u-picker
        :show="natureShow"
        title="求职类型"
        closeOnClickOverlay
        @cancel="natureShow = false"
        @close="natureShow = false"
        keyName="text"
        :columns="items_3"
        @confirm="selctChange3"
      >
      </u-picker>

      <view class="li-box fl-lr-c" @click="fnSelect(3)">
        <view class="name">求职类型</view>
        <view class="fl-t">
          <view class="content">
            {{ joblsit3text || "请选择" }}
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
    </view>
    <bottomBut @bottomBut="bottomBut" v-if="type == 2"></bottomBut>
  </view>
</template>

<script>
import myHead from "@/pages/component/baseHead.vue";
import bottomBut from "@/pages/component/bottomBut.vue";
import pickRegions2 from "@/pages/component/pick-regions2/pick-regions2.vue";
export default {
  data() {
    return {
      f_id: 0,
      title: "求职意向",
      type: 1, ////1add  2编辑   3首页
      cityShow: false, //期望城市
      cityVal: [""],
      salaryMin: "",
      salaryMax: "",
      salaryShow: false,
      initSalary: [],
      columns: [],
      columnData: [],
      natureShow: false, //性质
      natureVal: "",
      zwShow: false,

      zwVal: "2",

      hyVal: [],
      items_2: [[], []],
      columnDatad: [["2K"]],
      items5: [],
      items_4: [], //行业
      items_3: [],
      selRegion: "", //选择的省市区
      filter: ["重庆市", "上海市", "天津市", "北京市"],
      defaultRegion: ["", ""],
      isSel: false, //是否是选择

      IndustryIds: "", //公司行业 多个逗号
      IndustryTitles: "", //公司行业 多个逗号

      Category1: "",
      Category2: "",
      Category3: "",
      zwValid: "",
      zwValzhi: "",

      city: "",
      // 市
      shiid: "",
      //省
      shengid: "",
      //地区名称 拼接
      PcaAddress: "",

      // 当前id
      Id: 0,
      // 父id
      fuid: 0,

      resumeList: [],
    };
  },
  //子组件
  components: {
    myHead,
    bottomBut,
    pickRegions2,
  },
  watch: {
    selRegion: {
      handler(selRegion, oldRegion) {
        if (this.isSel) {
          if (this.filter.includes(selRegion[0])) {
            //存在
            if (selRegion[2] == "全部") {
              this.city = selRegion[0];
            } else {
              // console.log("相等")
              this.city = selRegion[0];
            }
          } else {
            if (selRegion[2] == "全部") {
              this.city = selRegion[1];
            } else {
              // console.log("不相等")
              this.city = selRegion[1];
            }
          }
        } else {
          if (this.filter.includes(selRegion[0])) {
            //存在
            this.city = selRegion[0];
            this.defaultRegion = [selRegion[0], selRegion[1]];
          } else {
            this.city = selRegion[1];
            this.defaultRegion = selRegion;
          }
        }
        //数据传出去
      },
      immediate: false,
    },
  },
  onLoad(o) {
    this.type = o.type;
    this.fuid = o.fuid;
    if (this.type == 2) {
      //3主页过来但是不显示删除
      o = JSON.parse(o.data);
      // console.log(o)
      this.Id = o.Id;
      this.zwValzhi = o.PositionName;
      this.zwValid = o.PositionId;
      this.Category1 = o.Category1;
      this.Category2 = o.Category2;
      this.Category3 = o.Category3;
      this.city = o.PcaAddress.split(" ")[o.PcaAddress.split(" ").length - 1];
      this.natureVal = o.WorkType + "";
      // 市
      this.shiid = o.CityCode;
      //省
      this.shengid = o.ProvinceCode;
      this.PcaAddress = o.PcaAddress;

      if (o.Negotiable) {
        this.salaryMin = "面议";
        this.salaryMax = "面议";
      } else {
        this.salaryMin = o.MinWage / 1000 + "K";
        this.salaryMax = o.MaxWage / 1000 + "K";
      }
      if (o.IndustryIds) {
        this.IndustryIds = o.IndustryIds;
        let did = o.IndustryIds.split(",");
        let dname = o.IndustryTitles.split(",");

        this.hyVal = [];

        did.forEach((res, index) => {
          let objtext = {
            value: res,
            text: dname[index],
          };
          this.hyVal.push(objtext);
        });
      } else {
        this.IndustryIds = o.IndustryIds;
      }

      this.IndustryTitles = o.IndustryTitles;
    }
    uni.$on("getHangyeSel", (res) => {
      if (res.length == 0) {
        this.IndustryIds = ""; //公司行业 多个逗号
        this.IndustryTitles = "不限";
        this.hyVal = [
          {
            value: "",
            text: "不限",
          },
        ];
      } else {
        let hyid = [];
        let hyname = [];
        this.hyVal = [];
        res.forEach((item) => {
          hyid.push(item.value);
          hyname.push(item.text);
          this.hyVal.push({
            value: item.value,
            text: item.text,
          });
        });
        this.IndustryIds = hyid.toString(); //公司行业 多个逗号
        this.IndustryTitles = hyname.toString(); //公司行业 多个逗号
      }
    });
    for (let j = 1; j <= 250; j++) {
      this.items_2[0].push(j + "K");
    }
    let data = [["2K"]];
    for (let s = 1; s <= 260; s++) {
      data.push([]);
      for (let i = 1; i < s; i++) {
        data[i].push(s + "K");
      }
    }
    this.columnDatad = data;
    this.$http.huirc.resumemy().then((res) => {
      if (res.Success) {
        res = res.Result;

        //处理数据
        this.resumeList = res.JobExpects; //基本信息
      }
    });
  },
  onUnload() {
    // console.log("onUnload")
    uni.$off("getHangyeSel");
  },
  computed: {
    lsit2text() {
      let arr = [];
      if (this.education.length > 0) {
        arr = this.education[0].filter((res) => {
          return this.value == res.value;
        });
      }
      if (arr.length == 0) {
        return "";
      } else {
        return arr[0].text || " ";
      }
    },
    joblsit3text() {
      let arr = [];
      if (this.items_3.length > 0) {
        arr = this.items_3[0].filter((res) => {
          return this.natureVal == res.value;
        });
      }
      if (arr.length == 0) {
        return "";
      } else {
        return arr[0].text || " ";
      }
    },
  },
  created() {
    this.xialadata();
  },
  // 计算方法
  methods: {
    changeHandler(e) {
      const {
        columnIndex,
        value,
        values, // values为当前变化列的数组内容
        index,
        // 微信小程序无法将picker实例传出来，只能通过ref操作
        picker = this.$refs.uPicker,
      } = e;
      // 当第一列值发生变化时，变化第二列(后一列)对应的选项
      if (columnIndex === 0) {
        // picker为选择器this实例，变化第二列对应的选项
        picker.setColumnValues(1, this.columnDatad[index]);
      }
    },
    onchange(e) {
      this.Category1 = "";
      this.Category2 = "";
      this.Category3 = "";

      // console.log("e", e);

      let OrgId = e.detail.value[e.detail.value.length - 1].value;
      // console.log("OrgId", OrgId);
      let Orgvalue = e.detail.value[e.detail.value.length - 1].text;
      // console.log("Orgvalue", Orgvalue);

      if (e.detail.value.length == 3) {
        this.Category1 = e.detail.value[0].value;
        this.Category2 = e.detail.value[1].value;
        this.Category3 = e.detail.value[2].value;
      }
      if (e.detail.value.length == 2) {
        this.Category1 = e.detail.value[0].value;
        this.Category2 = e.detail.value[1].value;
      }
      if (e.detail.value.length == 1) {
        this.Category1 = e.detail.value[0].value;
      }

      this.zwValid = OrgId;
      this.zwValzhi = Orgvalue;
    },

    showuseddepart() {
      this.$refs.picker.show();
    },

    xialadata() {
      // 期望职位
      this.$http.huirc.jobcategorytree().then((res) => {
        this.items5 = this.subTree(res.Result);
      });
      //求职类型
      this.$http.dic.DataSelector(385801442541701).then((res) => {
        if (res.Success) {
          // console.log("-------求职类型--------------");
          // console.log(res.Result);
          this.items_3 = this.xialist(res.Result);
        }
      });
    },
    //树形结构数据
    subTree(arr, fatherArr) {
      let totalArr;
      if (fatherArr !== undefined) {
        totalArr = fatherArr.children;
      } else {
        totalArr = [];
      }
      arr.forEach((item, index) => {
        let obj = {};
        obj.text = item.FullName;
        obj.value = item.Id;
        obj.children = [];
        if (item.Children !== null && item.Children.length != 0) {
          this.subTree(item.Children, obj);
        }
        totalArr.push(obj);
      });
      return totalArr;
    },
    // 下拉数据处理
    xialist(arr) {
      let drr = [];
      arr.forEach((res) => {
        let obj = {
          text: res.FullName,
          value: res.EnCode,
        };
        drr.push(obj);
      });
      return [drr];
    },

    initSalaryRange(e) {
      this.initSalary.push("面议");
      for (let i = 0; i < 100; i++) {
        this.initSalary.push(i + 1 + "k");
      }
      this.columns.push(this.initSalary);
      this.columns.push(["面议"]);
      this.columnData.push(["面议"]);
    },

    // 回调参数为包含columnIndex、value、values
    confirm(e) {
      this.salaryMin = e.value[0];
      this.salaryMax = e.value[1];
      this.salaryShow = false;
    },
    handleGetRegion(e, item) {
      //获取到的数据

      this.isSel = true;
      this.selRegion = e;
      // console.log(e)
      // console.log(item)

      // 市
      this.shiid = item[1].value;
      //省
      this.shengid = item[0].value;

      this.filter.some((res) => {
        if (res == e[0]) {
          this.PcaAddress = e[0];
          return true;
        } else {
          //地区名称 拼接
          this.PcaAddress = e.join("-");
        }
      });
    },

    ExpectedIndustry() {
      // 选择的数据
      let xzdata = JSON.stringify(this.hyVal);
      uni.navigateTo({
        url: `../ExpectedIndustry?hyVal=${xzdata}`,
      });
    },
    bottomBut() {
      let that = this;
      if (this.resumeList.length > 1) {
        uni.showModal({
          title: "删除提示",
          content: "确认要删除这条数据",
          success: function (res) {
            if (res.confirm) {
              let data = {
                Id: that.Id,
              };

              that.$http.huirc.resumejobexpectdel(data).then((res) => {
                if (res.Success) {
                  that.$util.toast("删除成功");
                  setTimeout(() => {
                    uni.navigateBack();
                  }, 1000);
                }
              });
            }
          },
        });
      } else {
        that.$util.toast("至少保留一个求职意向");
      }
    },
    clickRight() {
      if (!this.zwValzhi.length) {
        this.$util.toast("请选择期望职位");
        return;
      }
      // if (!this.IndustryTitles.length) {
      // 	this.$util.toast("请选择期望行业");
      // 	return;
      // }
      if (!this.PcaAddress.length) {
        this.$util.toast("请选择期望城市");
        return;
      }
      if (!this.salaryMin.length) {
        this.$util.toast("请选择薪资要求");
        return;
      }
      // console.log(this.natureVal);
      if (!this.natureVal.length > 0) {
        this.$util.toast("请选择求职类型");
        return;
      }

      if (this.type == 2) {
        //编辑修改
        let data = {
          Id: this.Id,
          ResumeId: this.fuid,
          PositionId: this.zwValid,
          PositionName: this.zwValzhi,
          CustomPositionName: "string",
          // WorkType: 0,

          Category1: this.Category1,
          Category2: this.Category2,
          Category3: this.Category3,

          IndustryIds: this.IndustryIds, //公司行业 多个逗号
          IndustryTitles: this.IndustryTitles, //公司行业 多个逗号

          //省
          ProvinceCode: this.shengid,
          // 市
          CityCode: this.shiid,
          // //地区名称 拼接
          PcaAddress: this.PcaAddress,
          // 区
          AreaCode: "string",
          MinWage:
            this.salaryMin == "面议" ? 0 : this.salaryMin.replace("K", "000"),
          MaxWage:
            this.salaryMax == "面议" ? 0 : this.salaryMax.replace("K", "000"),
          Negotiable: this.salaryMin == "面议" ? true : false,
          SortCode: 0,
          WorkType: this.natureVal,
        };
        console.log(data);
        // return;
        this.$http.huirc.resumejobexpectup(data).then((res) => {
          if (res.Success) {
            this.$util.toast("保存成功");
            if (this.type == 3) {
              uni.$emit("UpdateJobSearch");
            }
            setTimeout(() => {
              uni.navigateBack();
            }, 1600);
          }
        });
      } else {
        let data = {
          ResumeId: this.fuid,

          ResumeId: this.fuid,
          PositionId: this.zwValid,
          PositionName: this.zwValzhi,
          CustomPositionName: "string",
          WorkType: this.natureVal,
          Category1: this.Category1,
          Category2: this.Category2,
          Category3: this.Category3,
          IndustryIds: this.IndustryIds, //公司行业 多个逗号
          IndustryTitles: this.IndustryTitles ? this.IndustryTitles : "不限", //公司行业 多个逗号
          //省
          ProvinceCode: this.shengid,
          // 市
          CityCode: this.shiid,
          // //地区名称 拼接
          PcaAddress: this.PcaAddress,
          // 区
          AreaCode: "string",
          MinWage:
            this.salaryMin == "面议" ? 0 : this.salaryMin.replace("K", "000"),
          MaxWage:
            this.salaryMax == "面议" ? 0 : this.salaryMax.replace("K", "000"),
          Negotiable: this.salaryMin == "面议" ? true : false,
          SortCode: 0,
        };
        // console.log(data);
        // debugger
        this.$http.huirc.resumejobexpectcr(data).then((res) => {
          if (res.Success) {
            this.$util.toast("保存成功");
            if (this.type == 3) {
              uni.$emit("UpdateJobSearch");
            }
            setTimeout(() => {
              uni.navigateBack();
            }, 1600);
          }
        });
      }
    },
    fnSelect(type) {
      switch (type) {
        case 1:
          this.cityShow = true;
          break;
        case 2:
          this.salaryShow = true;
          break;
        case 3:
          this.natureShow = true;
          break;
        default:
      }
    },
    selctChange2(e) {
      this.salaryMin = e.value[0];
      this.salaryMax = e.value[1];
      this.salaryShow = false;
    },
    selctChange3(e) {
      this.natureVal = e.value[0].value;
      // console.log(this.natureVal, typeof this.natureVal);
      this.natureShow = false;
    },
  },
};
</script>


<style lang="scss" scoped>
.unidatapickert {
  position: fixed;
  left: -5000px;
  z-index: 99;
}

/deep/.uni-data-tree-dialog {
  top: 50% !important;
}

.li-box {
  background-color: #fff;

  .name {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: bold;
    color: #000000;
    flex-shrink: 0;
  }

  .content {
    flex: 1;
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: #999999;
    text-align: end;
    padding-left: 30rpx;
    box-sizing: border-box;
    overflow: hidden;
    word-break: break-all;
    /* break-all(允许在单词内换行。) */
    text-overflow: ellipsis;
    /* 超出部分省略号 */
    display: -webkit-box;
    /** 对象作为伸缩盒子模型显示 **/
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1; //两行
  }
}
</style>